<html>
<head>
<script>

Storage.prototype.setObject = function(key, value) {
  this.setItem(key, JSON.stringify(value));
}

Storage.prototype.getObject = function(key) {
  return JSON.parse(this.getItem(key));
}

var GM_xmlhttpRequest = function(obj) {
        var request=new XMLHttpRequest();
        request.onreadystatechange=function() { if(obj.onreadystatechange) { obj.onreadystatechange(request); }; if(request.readyState==4 && obj.onload) { obj.onload(request); } }
        request.onerror=function() { if(obj.onerror) { obj.onerror(request); } }
        try { request.open(obj.method,obj.url,true); } catch(e) { if(obj.onerror) { obj.onerror( {readyState:4,responseHeaders:'',responseText:'',responseXML:'',status:403,statusText:'Forbidden'} ); }; return; }
        if(obj.headers) { for(name in obj.headers) { request.setRequestHeader(name,obj.headers[name]); } }
        request.send(obj.data); return request;
}

function grabUpdateInfo() {
  localStorage.setObject('newRevList', '');
  localStorage.setObject('oldRevList', '');
  return;
  GM_xmlhttpRequest({
    method: 'GET',
    url: 'http://code.google.com/p/mwplayer/source/list',
    headers: {'Accept': 'application/atom+xml'},
    onload: function (responseDetails) {
      s = (responseDetails.responseText);
      var count = 0;
      var changes = 5;
      var newRevList = '';
      var oldRevList = '';
      while (count < changes + 5) {
        var a = s.indexOf('<td class="id">');
        a = a + 10;
        s = s.substring(a);
        var searchString = s.match(/href\x3D\x22detail\x3Fr\x3D([0-9]+)\x22\x3E/);
        var revisionString = RegExp.$1;
        var searchString = s.match(/a\sonclick\x3D\x22cancelBubble\x3Dtrue\x22\shref\x3D\x22detail\x3Fr\x3D[0-9]+\x22\x3E([\x21,\x22,\x23,\x26,\x27,\x28,\x29,\x2B,\x2C,\x2D,\x2E,\x2F,\x3A,\x3B,\x3F,\x5B,\x5D,\x5F,\x60,A-Z,a-z,0-9,\s]+)\x3C\x2Fa\x3E/);
        var commentString = RegExp.$1;
        var revLine = 'r' + revisionString + '... ' + commentString.replace('- ', '') + '<br>';
        if (count < changes)
          newRevList += revLine;
        else
          oldRevList += revLine;
        count++;
      }
      localStorage.setObject('newRevList', newRevList);
      localStorage.setObject('oldRevList', oldRevList);
    }
  });
}

chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
    var resp = {};
    if (request.action == "setGM") {
      for (var i in request) {
        if (i == 'action' || /function/i.test(i)) continue;
        localStorage.setObject(i, request[i]);
      }
    } else if (request.action == "getGM") {
      for (var i in request) {
        if (i == 'action' || /function/i.test(i)) continue;
        resp[i] = localStorage.getObject(i);
      }
    }

    // Grab update info
    if (request.newversion && localStorage.getObject('version') != localStorage.getObject('newversion')) {
      grabUpdateInfo();
    }

    sendResponse(resp);
  });

</script>
</head>
<body onload="">
</body>
</html>

